const KoaBouncer = require('koa-bouncer')
const {GETSTAFFS,DELSTAFFS,ADDSTAFFS,UPDATESTAFFS,SEARCHSTAFFS} = require('../service/staff.service')
module.exports = {
    // 获取员工
    getStaffs: async (ctx, next) => {
        await GETSTAFFS(ctx, next)
    },
    // 删除员工
    delStaffs: async (ctx, next) => {
        try {
            ctx.validateBody('id')
                .required('id必须得传值')
                .isString()
                .trim()
            ctx.validateBody('isDel')
                .required('isDel必须得传值')
                await DELSTAFFS(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
        
    },
    // 添加员工
    addStaffs: async (ctx, next) => {
        try {
            ctx.validateBody('id')
                .required('id必须得传值')
                .isString()
                .trim()
            ctx.validateBody('staffName')
                .required('istaffName必须得传值')
                .isString()
                .trim()
                ctx.validateBody('staffSex')
                .required('staffSex必须得传值')
                .isString()
                .trim()
                ctx.validateBody('staffPhone')
                .required('staffPhone必须得传值')
                .isString()
                .trim()
                ctx.validateBody('staffAge')
                .required('istaffAge必须得传值')
                .isString()
                .trim()
                ctx.validateBody('position')
                .required('position必须得传值')
                .isString()
                .trim()
                ctx.validateBody('department')
                .required('department必须得传值')
                .isString()
                .trim()
                ctx.validateBody('belongToShop')
                .required('belongToShop必须得传值')
                .isString()
                .trim()
                await ADDSTAFFS(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
    },
    // 更新员工
    updateStaffs: async (ctx, next) => {
        try {
            ctx.validateBody('id')
                .required('id必须得传值')
                .isString()
                .trim()
            ctx.validateBody('staffName')
                .required('istaffName必须得传值')
                .isString()
                .trim()
                ctx.validateBody('staffSex')
                .required('staffSex必须得传值')
                .isString()
                .trim()
                ctx.validateBody('staffPhone')
                .required('staffPhone必须得传值')
                .isString()
                .trim()
                ctx.validateBody('staffAge')
                .required('istaffAge必须得传值')
                ctx.validateBody('position')
                .required('position必须得传值')
                .isString()
                .trim()
                ctx.validateBody('department')
                .required('department必须得传值')
                .isString()
                .trim()
                await UPDATESTAFFS(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
    },
    searchStaffs: async (ctx, next) => {
        console.log(123);
        try {
            ctx.validateBody('keyWord')
                .required('id必须得传值')
                .isString()
                .trim()
            ctx.validateBody('belongToShop')
                .required('belongToShop必须得传值')
                await SEARCHSTAFFS(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
  
    },
}